using System;
using Common.Unity.Extensions.PropertyMapper.Policies;
using Microsoft.Practices.ObjectBuilder2;

namespace Common.Unity.Extensions.PropertyMapper
{
	public class DynamicPropertySetterManager : IDisposable
	{
		readonly IPropertySetterPolicy policy;
		readonly IPropertySetterInfo[] original;

		public DynamicPropertySetterManager( PropertyMappingContext context, IPropertySetterPolicy policy, params IPropertyMappingPolicy[] policies )
		{
			this.policy = policy;
			original = new IPropertySetterInfo[ policy.Properties.Count ];
			policy.Properties.CopyTo( original, 0 );
			foreach ( IPropertyMappingPolicy item in policies )
			{
				item.AddProperties( context, policy.Properties );
			}
		}

		void IDisposable.Dispose()
		{
			policy.Properties.Clear();
			foreach ( IPropertySetterInfo info in original )
			{
				policy.Properties.Add( info );
			}
		}
	}
}